package modules.majiang.viewManager
{
	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import communication.majiang.MajiangMsgEvent;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.entity.ActionType;
	import modules.entity.AutoActionType;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.MatchCurrentAction;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;
	
	import protobuf.MSPKG_INIT_TILES_NTF;
	import protobuf.MSPKG_NEED_DISCARD_NTF;
	import protobuf.MSPKG_SHOW_TILES_NTF;

	/**
	 *
	 * @author xumin.xu 2013-1-23
	 */
	public class ActionViewManager extends ViewManager
	{
		private var tableId:UInt64;

		private var actionNextViewManager:ActionNextViewManager;

		private var actionBetViewManager:ActionBetViewManager;

		private var back_btn:MovieClip;

		public function ActionViewManager(tableId:UInt64, tableView:MovieClip)
		{
			super();
			this.tableId = tableId;

			mainUI = tableView.actionView_mc;

			actionNextViewManager = new ActionNextViewManager(tableId, mainUI.actionNextView_mc);

			actionBetViewManager = new ActionBetViewManager(tableId, mainUI.betView_mc);

			mainUI.betView_mc.visible = false;
			mainUI.actionNextView_mc.visible = true;

			back_btn = tableView.back_btn;
			back_btn.visible = false;
			back_btn.addEventListener(MouseEvent.CLICK, onClick);

			mainUI.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			mainUI.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);

		}

		/**
		 * 当前游戏动作
		 **/
		private function get matchCurrentAction():MatchCurrentAction
		{
			return tableDataModel.matchCurrentAction;
		}

		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		private function onAddToStage(event:Event):void
		{
			//更新麻将桌子界面中显示
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, function(event:MajiangEvent):void
			{
				if (tableId.toString() != event.data.tableId.toString())
					return;
				updateIamBackState();
			});
			//第一轮的7张牌
			dispatcher.addEventListener(MSProto.MSID_INIT_TILES_NTF.toString(), onInitTilesNtf);

			//第一轮的要求押注请求
			dispatcher.addEventListener(MSProto.MSID_NEED_CALL_NTF.toString(), onNeedCallNtf);

			//要求客户端下注、过牌
			dispatcher.addEventListener(MSProto.MSID_NEED_BET_NTF.toString(), onNeedBetNtf);

			//赢牌的时候show自己的牌
			dispatcher.addEventListener(MSProto.MSID_SHOW_TILES_NTF.toString(), function(event:GameEvent):void
			{
				var pkg:MSPKG_SHOW_TILES_NTF = event.data;
				if(pkg.tableId.toString() != tableId.toString()) return;
				updateMatchAction();
			});

			//要求客户端出牌
			dispatcher.addEventListener(MSProto.MSID_NEED_DISCARD_NTF.toString(), onNeedDiscardNtf);

			//更新麻将桌子界面中显示
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateMajiangTableView);

			//广播更改sitout状态
			dispatcher.addEventListener(MSProto.MSID_SITOUT_NTF.toString(), function(event:GameEvent):void
			{
				if(tableId.toString() != event.data) return;
				updateIamBackState();
			});
			//比赛结束10后清空桌面
			dispatcher.addEventListener(MajiangEvent.CLEAR_TABLE_INFO, function(event:GameEvent):void
			{
				if(tableId.toString() != event.data) return;
				updateIamBackState();
			});
		}

		private function removeFromStage(event:Event):void
		{

		}

		private function onInitTilesNtf(event:GameEvent):void
		{
			var pkg:MSPKG_INIT_TILES_NTF = event.data;
			if (pkg.tableId.toString() != tableId.toString())
				return;
			mainUI.visible = true;
			actionNextViewManager.autoActionType = AutoActionType.NULL;
		}

		private function onNeedCallNtf(event:GameEvent):void
		{
			if(event.data != tableId.toString()) return;
			updateMatchAction();
		}

		private function onNeedBetNtf(event:GameEvent):void
		{
			if(event.data != tableId.toString()) return;
			updateMatchAction();
		}

		private function onNeedDiscardNtf(event:GameEvent):void
		{
			var pkg:MSPKG_NEED_DISCARD_NTF = event.data;
			if(pkg.tableId.toString() != tableId.toString()) return;
			mainUI.betView_mc.visible = false;
			mainUI.actionNextView_mc.visible = true;

			if (tableDataModel.sitChairId > 0)
			{
				mainUI.visible = true;
			}
		}

		private function onUpdateMajiangTableView(event:MajiangEvent):void
		{
			if (tableId.toString() != event.data.tableId.toString())
				return;
			updateMatchAction();
		}

		/**
		 * 更新当前游戏动作
		 **/
		private function updateMatchAction():void
		{
			//判断自己是否坐下
			mainUI.visible = tableDataModel.sitChairId > 0;
			if (tableDataModel.sitChairId < 0)
			{
				return;
			}

			mainUI.betView_mc.visible = false;
			mainUI.actionNextView_mc.visible = true;

			//如果轮到自己跟注或加注
			if (matchCurrentAction.currentAction == ActionType.CALL && tableDataModel.sitChairId == matchCurrentAction.currentChairId)
			{
				if (actionNextViewManager.autoActionType == AutoActionType.AUTOFOLD || actionNextViewManager.autoActionType == AutoActionType.AUTOFOLDORCHECK)
				{
					logger("自动弃牌");
					dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_FOLD_REQ.toString(), {tableId: tableId}));
				}
				else if (actionNextViewManager.autoActionType == AutoActionType.CALLANY)
				{
					logger("自动跟注");
					dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_CALL_REQ.toString(), {tableId: tableId}));
				}
				else
				{
					var callValue:Number = Math.min(matchCurrentAction.callMoney, tableDataModel.money);
					var showCallMoney:Number = Math.min(tableDataModel.money, matchCurrentAction.callMoney);

					actionBetViewManager.needCall(showCallMoney);

					mainUI.betView_mc.visible = true;
					mainUI.actionNextView_mc.visible = false;
				}

				actionNextViewManager.autoActionType = AutoActionType.NULL;
			}
			//要求客户端下注、过牌
			if (matchCurrentAction.currentAction == ActionType.BET && tableDataModel.sitChairId == matchCurrentAction.currentChairId)
			{
				if (actionNextViewManager.autoActionType == AutoActionType.AUTOFOLD)
				{
					logger("自动弃牌");
					dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_FOLD_REQ.toString(), {tableId: tableId}));
				}
				else if (actionNextViewManager.autoActionType == AutoActionType.AUTOCHECK || actionNextViewManager.autoActionType == AutoActionType.AUTOFOLDORCHECK || actionNextViewManager.autoActionType == AutoActionType.CALLANY)
				{
					logger("自动过牌");
					dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_PASS_REQ.toString(), {tableId: tableId}));
				}
				else
				{
					actionBetViewManager.needBet();

					mainUI.betView_mc.visible = true;
					mainUI.actionNextView_mc.visible = false;
				}

				actionNextViewManager.autoActionType = AutoActionType.NULL;
			}

			//判断玩家是否赢了或者输了或者弃牌了
			var isWin:Boolean = tableDataModel.isWin(tableDataModel.sitChairId);
			var isLose:Boolean = tableDataModel.isLose(tableDataModel.sitChairId);
			var isFold:Boolean = tableDataModel.isFold(tableDataModel.sitChairId);
			var isDraw:Boolean = tableDataModel.isDraw(tableDataModel.sitChairId);
			/**
			 * 当玩家赢牌、弃牌、输牌、手上没牌时不显示该界面
			 **/
			if (isWin || isLose || isFold || isDraw || tableDataModel.tilesInHand.length == 0)
			{
				mainUI.visible = false;
			}
		}

		private function updateIamBackState():void
		{
			var isSitOut:Boolean = tableDataModel.isSitout(tableDataModel.lookChairId);
			back_btn.visible = false;
			if (isSitOut && tableDataModel.playerTiles[tableDataModel.lookChairId].length < 1)
			{
				if (tableDataModel.sitChairId == tableDataModel.lookChairId)
				{
					back_btn.visible = true;
				}
			}
		}

		private function onClick(event:MouseEvent):void
		{
			var tableID:UInt64 = tableDataModel.tableDataFromLobby.tableID;
			var isSitOut:Boolean = false;

			dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_SITOUT_REQ.toString(), {tableId: tableID, isSitOut: isSitOut}));
		}
	}
}
